<template>
  <div style="min-height: 500px; padding: 0 30px;">

    <h2>管理个人信息</h2>
    <div style="margin: auto; width: 60%;">

      <div class="div-margin30">
        <el-form :model="form" :rules="rules" ref="editForm" label-width="70px" class="editForm">
          <el-form-item label="用户名">
            <el-input v-model="form.username" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select v-model="form.sex" placeholder="请选择性别">
              <el-option label="男" value="男"></el-option>
              <el-option label="女" value="女"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email">
            </el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pwd">
            <el-input v-model="form.pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="重复" prop="confirmPwd">
            <el-input v-model="form.confirmPwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="年级" prop="grade">
            <el-select v-model="form.grade" placeholder="请选择年级">
              <el-option label="本科" value="本科"></el-option>
              <el-option label="研一" value="研一"></el-option>
              <el-option label="研二" value="研二"></el-option>
              <el-option label="研三" value="研三"></el-option>
              <el-option label="博士" value="博士"></el-option>
              <el-option label="教师" value="教师"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <div class="div-margin20">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="danger" @click="reset">重置</el-button>
      </div>

    </div>

  </div>
</template>

<script>
  import {mapGetters} from 'vuex';
  import {Button, Form, FormItem, Input, Col, Row, Select, Option} from 'element-ui'
  import * as api_user from '../../api/user';

  export default {
    props: {},
    components: {
      'el-button': Button,
      'el-form': Form,
      'el-form-item': FormItem,
      'el-input': Input,
      'el-row': Row,
      'el-col': Col,
      'el-option': Option,
      'el-select': Select
    },
    data() {
      let ruleOfConfirmPwd = (rule, value, callback) => {
        if (value !== this.form.pwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        form: {
          name: '',
          sex: '',
          email: '',
          pwd: '',
          confirmPwd: '',
          grade: '',
        },
        rules: {
          name: [
            {required: true, message: '请输入姓名', trigger: 'change'},
          ],
          email: [
            {required: true, message: '请输入邮箱', trigger: 'change'},
            {type: 'email', message: '请输入正确的邮箱地址', trigger: 'change'}
          ],
          sex: [
            {required: true, message: '请选择性别', trigger: 'change'},
          ],
          grade: [
            {required: true, message: '请选择年级', trigger: 'change'},
          ],
          pwd: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 6, message: '最小长度为6', trigger: 'blur'}
          ],
          confirmPwd: [
            {required: true, message: '请确认密码', trigger: 'blur'},
            {validator: ruleOfConfirmPwd, trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    methods: {
      sendEdit(){
        api_user.editInfo(this.form).then(() => {
          this.$notify.success({
            title: '成 功 !',
            message: '个人信息已经修改'
          });
          this.reset();
        }).catch((err) => {
          this.$message.error("更新失败");
        });
      },
      save() {
        console.log('save');
        this.$refs['editForm'].validate((valid) => {
          if (valid) {
            console.log(' ⛅ ok');
            this.sendEdit();
          } else {
            this.$notify.error({
              title: '错 误 ！',
              message: '请重新检查'
            })
          }
        });
      },
      update() {
        this.$store.dispatch('getUserInfoByToken').then(() => {
            this.form.username = this.userInfo.username;
            this.form.sex = this.userInfo.sex;
            this.form.email = this.userInfo.email;
            this.form.name = this.userInfo.name;
            this.form.pwd = '';
            this.form.confirm = '';
            this.form.grade = this.userInfo.grade;
          })
          .catch((err) => {
            this.$message.error('获取个人信息失败：' + err.errmsg);
          });
      },
      reset() {
        this.update();
        this.form.pwd = '';
        this.form.confirmPwd = '';
      }
    },
    created() {

    },
    mounted() {
      this.update();
    }
  }
</script>

<style>
  .el-select {
    width: 100%;
  }
</style>
